//
//  ViewController.swift
//  whetherDemo
//
//  Created by 姚东 on 2025/2/19.
//

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    // 折线图
    @IBOutlet weak var polylinesView: UIView!
    // 表格
    @IBOutlet weak var tableView: UITableView!
    // 重载按钮
    @IBOutlet weak var realoadButton: UIButton!
    // 骨架
    @IBOutlet var skeletonView: UIView!
    
    var whetherTemperatureModel :WhetherTemperatureModel?
    
    var chartView: PolyLinesView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.realoadButton.isHidden = true
        // Do any additional setup after loading the view.
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.register(TimeTemperatureCell.self, forCellReuseIdentifier: "TimeTemperatureCell")
        self.chartView = PolyLinesView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 40, height: 300))
        self.polylinesView.addSubview(self.chartView!)
        reloadData()
    
    }

    
    @IBAction func reloadAction(_ sender: Any) {
        reloadData()
    }
    /// 刷新数据
    func reloadData(){
        NetworkManager.shared.getRequest(urlString: apiTemperature, responseType: WhetherTemperatureModel.self) { result in
            DispatchQueue.main.async {
                // Make sure this runs on the main thread
                self.skeletonView.isHidden = true
            }

            switch result {
             
            case .success(let whether):
                self.whetherTemperatureModel = whether
                DispatchQueue.main.async {
                    self.polylinesView.isHidden = false
                    self.tableView.isHidden = false
                    self.tableView.reloadData()
                    self.chartView!.setData(xValues: whether.hourly.time, yValues: whether.hourly.temperature_2m)
                }
             
                break
               
                
            case .failure(let error):
                print("请求失败: \(error.localizedDescription)")
                DispatchQueue.main.async {
                    self.realoadButton.isHidden = false
                    self.polylinesView.isHidden = true
                    self.tableView.isHidden = true
                }
              
            }
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return whetherTemperatureModel?.hourly.time.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TimeTemperatureCell", for: indexPath) as! TimeTemperatureCell
        if (whetherTemperatureModel == nil) {
            cell.configure(time: "", temperature: "")
        }
        else{
            let time = whetherTemperatureModel!.hourly.time[indexPath.row];
            let temperature = whetherTemperatureModel!.hourly.temperature_2m[indexPath.row]
            cell.configure(time: time, temperature: String(temperature))
        }
        return cell
    }

}

